<?php 
/**
* 
*/
class Test extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper('csv');
		$this->load->library('excel');
	}

	public function exportArrayCSV(){
		$array = array(
			array('Last Name', 'First Name', 'Gender'),
			array('Furtado', 'Nelly', 'female'),
			array('Twain', 'Shania', 'female'),
			array('Farmer', 'Mylene', 'female')
		);
	 
		
		echo array_to_csv($array);
		//array_to_csv($array, 'test.csv');
	}

	public function exportQueryCSV(){
		$query = $this->db->get('Employee');
	 
		//echo query_to_csv($query);
		query_to_csv($query, TRUE, 'test.csv');
	}

	public function exportExcel(){

		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle('test worksheet');
		//set cell A1 content with some text
		$this->excel->getActiveSheet()->setCellValue('A1', 'ทดสอบการจัดกึ่งกลางในแนวนอน');
		$this->excel->getActiveSheet()->setCellValue('A2', 'ทดสอบการจัดกึ่งกลางในแนวตั้ง');
		$this->excel->getActiveSheet()->setCellValue('B2', 'ทดสอบเปลี่ยนสีพื้นหลัง');
		$this->excel->getActiveSheet()->setCellValue('B3', 'ทดสอบเปลี่ยนสีตัวอักษร');
		//change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

		$this->excel->getActiveSheet()->mergeCells('A1:F1');

		$this->excel->getActiveSheet()->mergeCells('A2:A10');
		$this->excel->getActiveSheet()->mergeCells('B2:C2');
		
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->excel->getActiveSheet()->getStyle('A2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$this->excel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
			array(
					'type'       => PHPExcel_Style_Fill::FILL_SOLID,
					'startcolor' => array('rgb' => '008000'),
					'endcolor'   => array('rgb' => '008000')
				)
			);
		$this->excel->getActiveSheet()->getStyle("B3")->getFont()->getColor()->applyFromArray(array("rgb" => "FFA500"));
		 
		$filename='exel.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		             
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	public function exelCalculate(){

		$sheet = array(
		  array(
		    '1',
		    '2',
		    '3',
		    '4',
		  ),
		  array(
		    '5',
		    '6',
		    '7',
		    '8',
		  ),
		  array(
		    '9',
		    '10',
		    '11',
		    '12',
		  )
		);
		
		$this->excel->getActiveSheet()->setTitle('test formula cal');

		$this->excel->getActiveSheet()->fromArray($sheet, null, 'A1');

		$all_row = count( $sheet );
		for( $row=1; $row<=$all_row; $row++ ) {
      //$xlApp->ActiveSheet->Cells(1,1)->Value = "=Sum(A1:A10)";
      $this->excel->getActiveSheet()->setCellValue('E'.$row, '=SUM(A'.$row.',B'.$row.',C'.$row.',D'.$row.')');
    } 

    $this->excel->getActiveSheet()->setCellValue('H10', "1");
    $this->excel->getActiveSheet()->getStyle('H10')->getNumberFormat()->setFormatCode('00.00');

    

		$filename='testCal.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		             
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		$objWriter->save('php://output');
	}

	public function tablesorter(){

		$this->load->library('table');

		$tmpl = array ( 'table_open'  => '<table id="tb" class="table table-condensed table-hover tablesorter">' );
		$this->table->set_template($tmpl);

		$query = $this->db->query("SELECT * FROM Employee");
		$data['tablesorter'] = $this->table->generate($query);


		$this->load->view('include/header');
		$this->load->view('test',$data);
		$this->load->view('include/footer');


	}

}


?>